<?php
/**
 * Here the class '\UF\Console\Colorizer' is defined.
 *
 * @category   UniKap-Framework
 * @package    Console
 * @subpackage Core
 * @author     Ulf -UniKado- Kadner <ulfikado@gmail.com>
 * @since      2014-04-04 16:17
 * @version    0.1
 */

namespace UF\Console
{

    /**
     * This class gives you some simple static methods for coloring PHPs console output on unix systems.
     *
     * It uses ANSI escape sequences to colorize the output. It works fine on unixoid systems like Linux or Mac, but
     * the current windows console (also the powershell) does not support it.
     *
     * Windows user can use for this case {@link https://github.com/adoxa/ansicon/downloads AnsiCon} to extend CMD for
     * working with ANSI escape sequences. Simple download it and read the contained readme.txt for the simple
     * installation steps.
     *
     * @since  v0.1
     */
    final class Colorizer
    {

        # <editor-fold defaultstate="collapsed" desc="- - - -   P R I V A T E   S T A T I C   F I E L D S   - - - - - - - - - - - - - -">

        /**
         * @since  v0.1
         */
        private static $foreColors = [
            'black'       => '0;30',    'red'        => '0;31',    'lightRed'    => '1;31',     'green'     => '0;32',
            'lightGreen'  => '1;32',    'brown'      => '0;33',    'yellow'      => '1;33',     'blue'      => '0;34',
            'lightBlue'   => '1;34',    'purple'     => '0;35',    'lightPurple' => '1;35',     'cyan'      => '0;36',
            'lightCyan'   => '1;36',    'lightGray'  => '0;37',    'white'       => '1;37'
        ];

        /**
         * @since  v0.1
         */
        private static $backColors = [
            'black'       => '40',      'red'        => '41',      'green'       => '42',       'yellow'    => '43',
            'blue'        => '44',      'magenta'    => '45',      'cyan'        => '46',       'lightGray' => '47'
        ];

        # </editor-fold>

        # <editor-fold defaultstate="collapsed" desc="- - - -   P U B L I C   S T A T I C   M E T H O D S   - - - - - - - - - - - - - -">

        /**
         * Writes a string with the defined text color and the optional background color, to current console STDOUT.
         *
         * @param string $str       The string to write
         * @param string $foreColor The forecolor (text color) to use (please use one of the constants, defined by
         *                          {@see \UF\Console\ForeColors})
         * @param string $backColor The opt. background color to use (please use one of the constants, defined by
         *                          {@see \UF\Console\BackColors})
         * @since  v0.1
         */
        public static function Write( $str, $foreColor, $backColor = null )
        {
            $output = '';
            $useFg = ! empty( $foreColor ) && isset( self::$foreColors[$foreColor] );
            $useBg = ! empty( $backColor ) && isset( self::$backColors[$backColor] );
            if ( $useFg ) $output .= "\033[" . self::$foreColors[$foreColor] . 'm';
            if ( $useBg ) $output .= "\033[" . self::$backColors[$backColor] . 'm';
            echo $output, $str, ($useBg || $useFg) ? "\033[0m" : '';
        }

        /**
         * Writes a format with some replacements (like <code>\printf(...)</code>) with the defined text color and
         * the optional background color, to current console STDOUT.
         *
         * @param string $format    The formatdefinition like <code>'Jack is now %d years old!'</code>
         * @param array  $args      The replacements, to use inside $format, as a array.
         * @param string $foreColor The forecolor (text color) to use (please use one of the constants, defined by
         *                          {@see \UF\Console\ForeColors})
         * @param string $backColor The opt. background color to use (please use one of the constants, defined by
         *                          {@see \UF\Console\BackColors})
         * @since  v0.1
         */
        public static function WriteFormat( $format, array $args, $foreColor, $backColor = null )
        {
            if ( empty( $args ) ) self::Write ( $format, $foreColor, $backColor );
            else self::Write ( \vsprintf ( $format, $args ), $foreColor, $backColor );
        }

        /**
         * Writes a string with yellow text color to current console STDOUT.
         *
         * @param  string $str  The string to write
         * @param  array  $args The optional replacements, if $str is a format definition.
         * @since  v0.1
         */
        public static function EchoYellow( $str, array $args = [] )
        {
            self::WriteFormat( $str, $args, \UF\Console\ForeColors::YELLOW );
        }

        /**
         * Writes a string with red text color to current console STDOUT.
         *
         * @param  string $str  The string to write
         * @param  array  $args The optional replacements, if $str is a format definition.
         * @since  v0.1
         */
        public static function EchoRed( $str, array $args = [] )
        {
            self::WriteFormat( $str, $args, \UF\Console\ForeColors::RED );
        }

        /**
         * Writes a string with light red text color to current console STDOUT.
         *
         * @param  string $str  The string to write
         * @param  array  $args The optional replacements, if $str is a format definition.
         * @since  v0.1
         */
        public static function EchoLightRed( $str, array $args = [] )
        {
            self::WriteFormat( $str, $args, \UF\Console\ForeColors::LIGHTRED );
        }

        /**
         * Writes a string with green text color to current console STDOUT.
         *
         * @param  string $str  The string to write
         * @param  array  $args The optional replacements, if $str is a format definition.
         * @since  v0.1
         */
        public static function EchoGreen( $str, array $args = [] )
        {
            self::WriteFormat( $str, $args, \UF\Console\ForeColors::GREEN );
        }

        /**
         * Writes a string with light green text color to current console STDOUT.
         *
         * @param  string $str  The string to write
         * @param  array  $args The optional replacements, if $str is a format definition.
         * @since  v0.1
         */
        public static function EchoLightGreen( $str, array $args = [] )
        {
            self::WriteFormat( $str, $args, \UF\Console\ForeColors::LIGHTGREEN );
        }

        /**
         * Writes a string with blue text color to current console STDOUT.
         *
         * @param  string $str  The string to write
         * @param  array  $args The optional replacements, if $str is a format definition.
         * @since  v0.1
         */
        public static function EchoBlue( $str, array $args = [] )
        {
            self::WriteFormat( $str, $args, \UF\Console\ForeColors::BLUE );
        }

        /**
         * Writes a string with light blue text color to current console STDOUT.
         *
         * @param  string $str  The string to write
         * @param  array  $args The optional replacements, if $str is a format definition.
         * @since  v0.1
         */
        public static function EchoLightBlue( $str, array $args = [] )
        {
            self::WriteFormat( $str, $args, \UF\Console\ForeColors::LIGHTBLUE );
        }

        /**
         * Writes a string with purple text color to current console STDOUT.
         *
         * @param  string $str  The string to write
         * @param  array  $args The optional replacements, if $str is a format definition.
         * @since  v0.1
         */
        public static function EchoPurple( $str, array $args = [] )
        {
            self::WriteFormat( $str, $args, \UF\Console\ForeColors::PURPLE );
        }

        /**
         * Writes a string with light purple text color to current console STDOUT.
         *
         * @param  string $str  The string to write
         * @param  array  $args The optional replacements, if $str is a format definition.
         * @since  v0.1
         */
        public static function EchoLightPurple( $str, array $args = [] )
        {
            self::WriteFormat( $str, $args, \UF\Console\ForeColors::LIGHTPURPLE );
        }

        /**
         * Writes a string with cyan text color to current console STDOUT.
         *
         * @param  string $str  The string to write
         * @param  array  $args The optional replacements, if $str is a format definition.
         * @since  v0.1
         */
        public static function EchoCyan( $str, array $args = [] )
        {
            self::WriteFormat( $str, $args, \UF\Console\ForeColors::CYAN );
        }

        /**
         * Writes a string with light cyan text color to current console STDOUT.
         *
         * @param  string $str  The string to write
         * @param  array  $args The optional replacements, if $str is a format definition.
         * @since  v0.1
         */
        public static function EchoLightCyan( $str, array $args = [] )
        {
            self::WriteFormat( $str, $args, \UF\Console\ForeColors::LIGHTCYAN );
        }

        /**
         * Writes a string with light gray text color to current console STDOUT.
         *
         * @param  string $str  The string to write
         * @param  array  $args The optional replacements, if $str is a format definition.
         * @since  v0.1
         */
        public static function EchoGray( $str, array $args = [] )
        {
            self::WriteFormat( $str, $args, \UF\Console\ForeColors::LIGHTGRAY );
        }

        # </editor-fold>

        # <editor-fold defaultstate="collapsed" desc="- - - >   H I D D E N   C O N S T R U C T O R   - - - - - - - - - - - - - - - - -">

        /**
         * Hidden because the class is only for static use.
         *
         * @since  v0.1
         */
        private function __construct() { }

        # </editor-fold>

    }

}

